<?php
session_start();
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );

/**
 * The main runtime of the application.
 * 
 * @author Niels Janssen
 * @since 2009-04-20
 * @static 
 */
class Application
{
	/**
	 * The external configuration of the soap url. Must reference to an active SugarCRM installation soap.php.
	 *
	 * @var string
	 */
	public static $sugarSoapURL = '';
	
	/**
	 * The internal reference to this object.
	 *
	 * @var Application
	 */
	private static $instance = null;
	
	/**
	 * The soap client to connect to the sugar crm client.
	 *
	 * @var SoapClient
	 */
	private $sugarSoapBinding = null;
	
	/**
	 *  Private to prevent external initialization.
	 */
	private function __construct() {}
	
	/**
	 * Gets the soap binding, initializes a new binding if no other exists.
	 *
	 * @return SoapClient
	 */
	public function getSoapBinding()
	{
		if( $this->sugarSoapBinding == null )
		{
			if( empty( self::$sugarSoapURL ) )
			{
				throw new InvalidArgumentException( 'No Sugar soap connection URL was set' );
			}
			
			$this->sugarSoapBinding = new nusoap_client(self::$sugarSoapURL, true );
		}
		
		return $this->sugarSoapBinding;
	}
	
	/**
	 * Returns a reference to this class
	 *
	 * @return Application
	 */
	public static function getInstance()
	{
		if( self::$instance == NULL )
		{
			self::$instance = new self();
		}
		
		return self::$instance;
	}
	
	/**
	 * Initializes the autoloader, starts the connection to the backend and start the default
	 * router.
	 *
	 */
	public static function run()
	{
		$application = self::getInstance();

		//spl_autoload_register( array( 'Application', 'import' ) );
		set_exception_handler( array( $application, 'handleException' ) );
		
		SoapModel::setSoapBinding( $application->getSoapBinding() );

		$controller = new ApplicationController();
		$controller->defaultAction();
	}
	
	/**
	 * Holds the class locations.
	 *
	 * @var array
	 */
	private static $classList = array(
		'Account'						=> 'System/Models',
		'ApplicationController'			=> 'System',
		'Bedrijf'						=> 'System/Models',
		'BedrijfController'				=> 'System/Controllers',
		'Bedrijfsbegeleider'			=> 'System/Models',
		'BedrijfsbegeleiderController'	=> 'System/Controllers',
		'Controller'					=> 'System',
		'DefaultController'				=> 'System/Controllers',
		'Docent'						=> 'System/Models',
		'DocentController'				=> 'System/Controllers',
		'ElementValidation'				=> 'System/Util/Form',
		'ElementValidationException'	=> 'System/Util/Form',
		'FieldSet'						=> 'System/Util/Form/Containers',
		'Form'							=> 'System/Util/Form',
		'FormContainer'					=> 'System/Util/Form',
		'FormElement'					=> 'System/Util/Form',
		'FormException'					=> 'System/Util/Form',
		'FormNode'						=> 'System/Util/Form',
		'Button'						=> 'System/Util/Form/Elements',
		'Checkbox'						=> 'System/Util/Form/Elements',
		'InputFile'						=> 'System/Util/Form/Elements',
		'InputHidden'					=> 'System/Util/Form/Elements',
		'InputPassword'					=> 'System/Util/Form/Elements',
		'InputText'						=> 'System/Util/Form/Elements',
		'RadioButton'					=> 'System/Util/Form/Elements',
		'nusoap_client'					=> 'System/Util/NuSoap/nusoap.php',
		'nusoap_base'					=> 'System/Util/NuSoap/nusoap.php',
		'PlainText'						=> 'System/Util/Form/Elements',
		'SelectOneChoice'				=> 'System/Util/Form/Elements',
		'SubmitButton'					=> 'System/Util/Form/Elements',
		'TextArea'						=> 'System/Util/Form/Elements',
		'RadioButtonContainer'			=> 'System/Util/Form/Containers',
		'HTMLView'						=> 'System/Views',
		'Inflector'						=> 'System/Util',
		'Smarty'						=> 'System/Util/Smarty/libs',
		'SoapField'						=> 'System/Util',
		'SoapModel'						=> 'System/Util',
		'SoapResultSet'					=> 'System/Util',
		'Student'						=> 'System/Models',
		'User'							=> 'System/Models',
		'View'							=> 'System/Views',
		'ViewManager'					=> 'System/Views',
	
		'Opdracht'						=> 'System/Models',
		'OpdrachtController'			=> 'System/Controllers',
		'StudentOpdrachtController'		=> 'System/Controllers'
	);
	
	/**
	 * Provides a static import method. This loads the required class using the internal
	 * classList variable.
	 *
	 * @param string $class
	 */
	public static function import( $class )
	{		
		$fileName = self::$classList[ $class ];
		$fileName .= substr( $fileName, -4 ) == '.php' ? '' :  '/' . $class . '.php';

		if( file_exists( $fileName ) )
			require $fileName;
	}
	
	public function handleException( Exception $exception )
	{
		echo '<h1>Het ging dood</h1>';
		echo '<blockquote><pre>' . $exception->__toString() . '</pre></blockquote>';
	}
}

function __autoload( $class )
{
	Application::import( $class );
}

//Application::$sugarSoapURL = 'http://cyclon.nielsjanssen.eu/sugar/soap.php?wsdl';
Application::$sugarSoapURL = 'http://www.vddijssel.nl/crm/Niels_Janssen/soap.php?wsdl';
//Application::$sugarSoapURL = 'http://127.1.1.1/sugar/soap.php?wsdl';
Application::run();
